module ICarry {

    export class CarrierProfileViewModel implements PrismBreak.PbViewModel {

        private _app: ICarryApplication;
        private _view: PrismBreak.View;

        private nextToken: string;
        private previousToken: string;

        private carrier: KnockoutObservable<ICarry.Carrier>;

        constructor(app: PrismBreak.PbApp) {

            this._app = <ICarryApplication>app;

            this._app.EventManager.Subscribe(ICarry.ICarryEvent[ICarry.ICarryEvent.SetCarrierInformation], (carrier: ICarry.Carrier) => {
                this.SetCarrierInfo(carrier);
            });

        }

        public Initialize(view: PrismBreak.View) {

            //Defaults

            this.carrier = ko.observable<ICarry.Carrier>();

            ko.applyBindings(this, view.Node);

            this._view = <PrismBreak.View> view;
        }

        public Activated() {
            //Start listening to next event
            this.nextToken = this._app.EventManager.Subscribe(PrismBreak.EventType[PrismBreak.EventType.RequestNextStep], () => {
                this.GoToNextStep();
            });

            this.previousToken = this._app.EventManager.Subscribe(PrismBreak.EventType[PrismBreak.EventType.RequestPreviousStep], () => {
                this.GoToPreviousStep();
            });

            this._app.EventManager.Publish(ICarry.ICarryEvent[ICarry.ICarryEvent.NextButtonVisibilityChange], false);

        }


        private GoToNextStep(): void {

            if (this.CanGoNext()) {
                //Stop listening to next event
                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestNextStep], this.nextToken);
                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestPreviousStep], this.previousToken);
                //activate next view


            }
        }

        private CanGoNext(): boolean {
            //TODO: do some validation
            return true;
        }

        private GoToPreviousStep(): void {

            if (this.CanGoPrevious()) {
                //Stop listening to previous event
                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestNextStep], this.nextToken);
                this._app.EventManager.Unsubscribe(PrismBreak.EventType[PrismBreak.EventType.RequestPreviousStep], this.previousToken);

                this._app.EventManager.Publish(ICarry.ICarryEvent[ICarry.ICarryEvent.NextButtonVisibilityChange], true);

                //activate previous view
                this._app.ViewManager.ActivateViewById("RequestView");


            }
        }

        private CanGoPrevious(): boolean {
            //TODO: do some validation
            return true;
        }

        private SetCarrierInfo(carrier: ICarry.Carrier): void {

            this.carrier(carrier);
        }

        private AskCarrier(): void {


        }

    }

    export class CarrierInfo {

        public Name: KnockoutObservable<string>;
        public Checked: KnockoutObservable<boolean>;

        constructor(infoName?: string, checked?: any) {

            this.Name = ko.observable(infoName);
            this.Checked = ko.observable(checked);
        }
    }

}  